package com.coffee.admin.service.common.impl;

import com.coffee.admin.api.common.query.GtRegionQuery;
import com.coffee.admin.api.common.result.AdminMenuResult;
import com.coffee.admin.api.common.result.GtRegionResult;
import com.coffee.admin.api.common.service.GtRegionService;
import com.coffee.admin.api.common.service.RedisService;
import com.coffee.admin.api.platform.query.CarouselFigureQuery;
import com.coffee.admin.service.common.bean.AdminMenu;
import com.coffee.admin.service.common.bean.GtRegion;
import com.coffee.admin.service.common.dao.GtRegionDao;
import com.coffee.admin.service.platform.dao.CarouselFigureDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.sku.api.sku.query.RecommendSkuQuery;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.api.sku.result.SearchListResult;
import com.coffee.sku.service.sku.bean.RecommendSku;
import com.coffee.sku.service.sku.dao.RecommendSkuDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class GtRegionServiceImpl implements GtRegionService {
    @Resource
    private GtRegionDao gtRegionDao;
    @Resource
    private RecommendSkuDao recommendSkuDao;
    @Resource
    private CarouselFigureDao carouselFigureDao;
    @DubboReference
    private RedisService redisService;

    @Override
    public Page<GtRegionResult> getPager(GtRegionQuery param, PagerInfo pr) {
        Page<GtRegionResult> pager = new Page<GtRegionResult>(pr.getStart(), pr.getPageSize());
        Integer count = gtRegionDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<GtRegion> list = gtRegionDao.page(param, pr.getStart(), pr.getPageSize());
            List<GtRegionResult> gtRegionResults = DTOUtils.convertList(list, GtRegionResult.class);
            for (GtRegionResult gtRegionResult : gtRegionResults) {
                SkuListReq skuListReq = new SkuListReq();
                skuListReq.setIsEnable("Y");
                skuListReq.setIsDisable("N");
                skuListReq.setIsApprove("Y");
                skuListReq.setIsDelete("N");
                skuListReq.setCityId(gtRegionResult.getId());
                Integer count1 = recommendSkuDao.skuCount(skuListReq);
                gtRegionResult.setRecommendCount(count1);
                CarouselFigureQuery carouselFigureQuery = new CarouselFigureQuery();
                carouselFigureQuery.setCityId(gtRegionResult.getId());
                Integer integer = carouselFigureDao.countByCity(carouselFigureQuery);
                gtRegionResult.setCarouselCount(integer);
            }
            pager.setData(gtRegionResults);
        }
        return pager;
    }

    @Override
    public Integer count(GtRegionQuery param) {
        Integer count = gtRegionDao.count(param);
        return count;
    }

    @Override
    public GtRegionResult getDetail(Integer id) {
        GtRegion item = gtRegionDao.findById(id);
        if (item == null) {
            return null;
        }
        GtRegionResult result = new GtRegionResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<GtRegionResult> getList(GtRegionQuery param, PagerInfo pr) {
        List<GtRegion> list = gtRegionDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, GtRegionResult.class);
    }

    @Override
    public List<GtRegionResult> getList(GtRegionQuery param) {
        List<GtRegionResult> gtRegionResults = new ArrayList<>();
        List<GtRegion> list = gtRegionDao.list(param);
        //分组
        Map<Integer, List<GtRegion>> GtRegionMap = list.stream().collect(Collectors.groupingBy(GtRegion::getPid));
        List<GtRegion> gtRegions = GtRegionMap.get(0);
        gtRegionResults = DTOUtils.convertList(gtRegions, GtRegionResult.class);
        recursion(GtRegionMap, gtRegionResults);
        return gtRegionResults;
    }

    @Override
    public List<GtRegionResult> getList() {
        List<GtRegionResult> gtRegionResults = new ArrayList<>();
        Object address = redisService.get("address");
        if (address != null) {
            try {
                if (address instanceof ArrayList<?>) {
                    for (Object o : (List<?>) address) {
                        gtRegionResults.add(GtRegionResult.class.cast(o));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return gtRegionResults;
        }
        GtRegionQuery param = new GtRegionQuery();
        List<GtRegion> list = gtRegionDao.list(param);
        //分组
        Map<Integer, List<GtRegion>> GtRegionMap = list.stream().collect(Collectors.groupingBy(GtRegion::getPid));
        List<GtRegion> gtRegions = GtRegionMap.get(0);
        gtRegionResults = DTOUtils.convertList(gtRegions, GtRegionResult.class);
        recursion(GtRegionMap, gtRegionResults);
        redisService.set("address", gtRegionResults);
        return gtRegionResults;
    }


    public void recursion(Map<Integer, List<GtRegion>> groupListMap, List<GtRegionResult> adminMenuResults) {
        for (GtRegionResult adminMenuResult : adminMenuResults) {
            Integer id = adminMenuResult.getId();
            List<GtRegion> adminMenus = groupListMap.get(id);
            if (CollectionUtils.isNotEmpty(adminMenus)) {
                List<GtRegionResult> submenuList = DTOUtils.convertList(adminMenus, GtRegionResult.class);
                recursion(groupListMap, submenuList);
                adminMenuResult.setChildList(submenuList);
            }
        }
    }


    @Override
    public void delete(Integer id) {
        gtRegionDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(GtRegionResult result) throws BusinessException {
        GtRegion item = new GtRegion();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            gtRegionDao.insert(item);
        } else {
            GtRegion tmp = gtRegionDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            gtRegionDao.updateById(item);
        }
    }

    @Override
    public void updateIsMustCity(GtRegionResult result) throws BusinessException {
        GtRegion gtRegion = gtRegionDao.findById(result.getId());
        if (gtRegion.getPid() != 0) {
            GtRegion pidGtRegion = new GtRegion();
            pidGtRegion.setId(gtRegion.getPid());
            pidGtRegion.setIsMustCity("Y");
            gtRegionDao.updateById(pidGtRegion);
        }
        GtRegion gtRegionUpdate = new GtRegion();
        gtRegionUpdate.setId(gtRegion.getId());
        gtRegionUpdate.setIsMustCity("Y");
        gtRegionDao.updateById(gtRegionUpdate);
    }

    @Override
    public Map<Integer, GtRegionResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, GtRegionResult> map = new HashMap<Integer, GtRegionResult>();
        List<GtRegion> list = gtRegionDao.findByIds(StringUtil.setToList(idSet));
        List<GtRegionResult> resultList = DTOUtils.convertList(list, GtRegionResult.class);
        for (GtRegionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, GtRegionResult> getItemMap() {
        Map<Integer, GtRegionResult> map = new HashMap<Integer, GtRegionResult>();
        GtRegionQuery param = new GtRegionQuery();
        List<GtRegion> list = gtRegionDao.page(param, 0, Integer.MAX_VALUE);
        List<GtRegionResult> resultList = DTOUtils.convertList(list, GtRegionResult.class);
        for (GtRegionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
